package easy.trace.wrapper;


import easy.trace.Span;
import easy.trace.agent.Tracer;
import java.util.concurrent.Callable;

public class TraceCallable<V> implements Callable<V> {
    private final Callable<V> impl;
    private final Span parent;
    private final Tracer tracer = Tracer.getTracer();

    public TraceCallable(Callable<V> impl) {
        this.parent = this.tracer.getParentSpan();
        this.impl = impl;
    }

    public TraceCallable(Span parent, Callable<V> impl) {
        this.impl = impl;
        this.parent = parent;
    }

    @Override
    public V call() throws Exception {
        if (this.parent != null) {
            this.tracer.setParentSpan(this.parent);
        }

        return this.impl.call();
    }

    public Callable<V> getImpl() {
        return this.impl;
    }
}

